<?php

if ( !defined("XOOPS_ROOT_PATH") ) exit;
if ( !class_exists("xoGroups") ) {
	include dirname(dirname(__FILE__))."/include/groups.php";
}
class groupsPermission extends XoopsObject 
{
	public function __construct() {
		$this->initVar("perm_id", XOBJ_DTYPE_INT, null, false);
		$this->initVar("group_id", XOBJ_DTYPE_INT);
		$this->initVar("perm_key",XOBJ_DTYPE_TXTBOX);
		$this->initVar("perm_value",XOBJ_DTYPE_TXTBOX);
	}
		
}

class groupsPermissionHandler extends XoopsPersistableObjectHandler
{
    private $_perm = array(
                        'access_perm'   => 0,
                        'post_perm'     => 1,
                        'reply_perm'    => 1,
                        'join_perm'     => 0,
                        'footprint_perm'=> 0,
                    );
    
	public function __construct($db) {
        parent::__construct($db,"groups_permission","groupsPermission","perm_id");
    }
    
    /**
     * create group default permission
     * permission categroy
     * 'access_perm'=> ('0','1','2')    // 0 任何用户（含游客） 1 组成员 2 组管理员
     * 'post_perm'=>('0','1','2')       // 0 用户可以发表 1 组成员 2 组管理员
     * 'reply_perm'=>('0','1','2')      // 0 任何注册用户 1 组成员 2 组管理员
     * 'join_perm'=>('0','1','2')       // 0 任何注册用户加入不需要审核 1 加入需要审核 2 内部群，不能加入
     * 'footprint_perm'=>('0','1')      // 0 自动记录 1 用户创建
     * 
     * @param int $group_id
     * @return int $group_id
     */
    public function setGroupDefPerm( $group_id ) {
        foreach ( $this->_perm as $k=>$val ) {
            $obj = $this->get();
            $obj->setVar("group_id",$group_id);
            $obj->setVar("perm_key",$k);
            $obj->setVar("perm_value",intval($val));
            $this->insert($obj);
        }
        return $group_id;
    }
    
	public function getDefAccessOptions() {
		return array(
			"0"=>"任何用户可以访问（含游客）",
			"1"=>"群组成员可以访问",
			"2"=>"非公开群"
		);
	}
	
	public function getDefPostOptions() {
		return array(
			"0"=>"任何用户可以发表",
			"1"=>"群组成员可以发表",
			"2"=>"仅管理员可以发表"
		);
	}
	
	public function getDefReplyOptions() {
		return array(
			"0"=>"任何用户可以回复",
			"1"=>"群组成员可以回复",
			"2"=>"仅管理员可以回复"
		);
	}
	
	public function getDefJoinOptions() {
		return array(
			"0"=>"任何用户可以加入",
			"1"=>"加入需要审核",
			"2"=>"内部群，不能加入"
		);
	}
	
	public function getDefFootprintOptions() {
		return array(
			"0"=>"来访自动记录",
			"1"=>"用户手动触发",
		);
	}
    /**
     * get group permission
     *
     * @param int $group_id
     * @param string $perm_key
     * @return array $ret
     */
    public function getGroupPerm( $group_id, $perm_key=null ) {
        if ( empty($group_id) ) return false;
        $ret = array();
        $criteria = new CriteriaCompo(new Criteria("group_id",$group_id));
        
        if ( !empty($perm_key) && in_array($perm_key, $this->_perm) ) {
            $criteria->add( new Criteria("perm_key",$perm_key) );
        }
        
        $objs = $this->getAll($criteria);
        if ( $objs ) {
            foreach ( $objs as $k=>$obj ) {
                $ret[$obj->getVar("perm_key","n")] = $obj->getVar("perm_value","n");
            }
        }
        return $ret;
    }
    
    /**
     * update group permission
     * $params = array(
     *     "group_id"=>$group_id,
     *     "perm" => array(
     *          "1" => array("access_perm"=>"0"),
     *          "2" => array("post_perm"=>"0"),
     *          "3" => array("reply_perm"=>"0"),
     *          "4" => array("join_perm"=>"0"),
     *          "5" => array("footprint_perm"=>"0"),
     *      )
     * }
     * @param array $params
     * @return bool $ret
     */
    public function updateGroupPerm( array $params ) {
        extract($params);
		$myts =  MyTextSanitizer::getInstance();
        if ( empty($group_id) || ( empty($perm) || !is_array($perm) ) ) return false;
		foreach ( $perm as $key => $val ) {
			if ( !isset($this->_perm[$key]) || $myts->stripSlashesGPC($val) == $this->_perm[$key] ) {
				unset($perm[$key]);
			}
		}
		foreach ( $perm as $k=>$val ) {
			$criteria = new CriteriaCompo(new Criteria("group_id",$group_id));
			$criteria->add( new Criteria("perm_key",$k) );
			$this->updateAll("perm_value",$val,$criteria);
        }
        return $group_id;
    }
}